package edu.ynu.se.xiecheng.controller;

import edu.ynu.se.xiecheng.entity.CameraRecord;
import edu.ynu.se.xiecheng.entity.SoilData;
import edu.ynu.se.xiecheng.service.DeviceService;
import edu.ynu.se.xiecheng.dto.SensorDataDTO;
import edu.ynu.se.xiecheng.dto.DeviceDTO;
import edu.ynu.se.xiecheng.dto.ChartDataDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备控制器
 */
@RestController
@RequestMapping("/api/devices")
@CrossOrigin
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    /**
     * 获取大棚内的摄像头设备列表
     */
    @GetMapping("/cameras")
    public ResponseEntity<List<DeviceDTO>> getCameraDevices(
        @RequestParam(required = false) Long greenhouseId,
        @RequestParam(required = false, defaultValue = "2") Long deviceTypeId) {
        List<DeviceDTO> cameras;
//        if (greenhouseId != null) {
//            cameras = deviceService.findByDeviceTypeAndGreenhouseId(deviceTypeId, greenhouseId);
//        } else {
            cameras = deviceService.findByDeviceType(deviceTypeId);
//        }
        return ResponseEntity.ok(cameras);
    }

    /**
     * 获取设备的传感器数据
     */
    @GetMapping("/{deviceId}/sensor-data")
    public ResponseEntity<SensorDataDTO> getSensorData(@PathVariable Long deviceId) {
        SensorDataDTO data = deviceService.getSensorData(deviceId);
        return data != null ? ResponseEntity.ok(data) : ResponseEntity.notFound().build();
    }

    /**
     * 获取设备的图表数据
     */
    @GetMapping("/{deviceId}/chart-data")
    public ResponseEntity<ChartDataDTO> getChartData(
            @PathVariable Long deviceId,
            @RequestParam(defaultValue = "6") Integer limit) {
        ChartDataDTO data = deviceService.getChartData(deviceId, limit);
        return data != null ? ResponseEntity.ok(data) : ResponseEntity.notFound().build();
    }

    /**
     * 获取摄像头历史记录
     */
    @GetMapping("/{deviceId}/camera-records")
    public ResponseEntity<List<CameraRecord>> getCameraRecords(@PathVariable Long deviceId) {
        return ResponseEntity.ok(deviceService.getCameraRecords(deviceId));
    }

    /**
     * 获取设备的历史数据
     */
    @GetMapping("/{deviceId}/history")
    public ResponseEntity<List<SensorDataDTO>> getDeviceHistory(
            @PathVariable Long deviceId,
            @RequestParam(defaultValue = "day") String timeRange,
            @RequestParam(required = false) Integer limit) {
        return ResponseEntity.ok(deviceService.getDeviceHistory(deviceId, timeRange, limit));
    }

    /**
     * 获取视频流
     */
    @GetMapping("/{deviceId}/video-stream")
    public ResponseEntity<String> getVideoStream(@PathVariable Long deviceId) {
        // 这里返回视频流地址，实际项目中可能是RTSP或HLS流地址
        String streamUrl = null;
        return streamUrl != null ? ResponseEntity.ok(streamUrl) : ResponseEntity.notFound().build();
    }
} 